package com.jdy.haoduoaiteacher.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.RelativeLayout;

import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.animation.ValueAnimator;
import com.nineoldandroids.animation.ValueAnimator.AnimatorUpdateListener;
import com.ycsj.goldmedalnewconcept.R;

/**
 * 
 * @author hufei
 * 
 */
public class MusicPlayView extends RelativeLayout {

  @SuppressWarnings("unused")
  private final Context mContext;
  // 旋转一周所用时间
  private static final int ROTATE_TIME = 12 * 1000;
  // 动画旋转重复执行的次数，这里代表无数次，似乎没有无限执行的属性，所以用了一个大数字代表
  private static final int ROTATE_COUNT = 10000;
  private View mAvatar = null;
  private boolean isPlay = false;
  ObjectAnimator mAniAvatar;

  float mValueAvatar;
  float mValueDisc;
  float mValueNeedle;

  public MusicPlayView(Context context, AttributeSet attrs) {
    super(context);
    mContext = context;
  }

  public MusicPlayView(Context context) {
    super(context);
    mContext = context;
  }

  public MusicPlayView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    mContext = context;
  }

  /**
   * Where the media player is playing!
   * 
   * @return
   */
  public boolean isPlay() {
    return isPlay;
  }

  public void setPlay(boolean isPlay) {
    this.isPlay = isPlay;
  }

  @Override
  protected void onFinishInflate() {
    super.onFinishInflate();

    mAvatar = findViewById(R.id.avatar);
    initAvatarAnimation(0f);

  }

  /**
   * 播放
   */
  public void play() {
    AnimatorSet animSet = new AnimatorSet();
    animSet.play(mAniAvatar);
    animSet.start();
    setPlay(true);
  }

  /**
   * 暂停
   */
  public void pause() {
    mAniAvatar.cancel();
    initAvatarAnimation(mValueAvatar);
    setPlay(false);
  }

  /**
   * 初始化旋转封面动画对象
   * 
   * @param start
   */
  private void initAvatarAnimation(float start) {
    mAniAvatar = ObjectAnimator.ofFloat(mAvatar, "rotation", start,
        360f + start);
    mAniAvatar.addUpdateListener(new AnimatorUpdateListener() {
      @Override
      public void onAnimationUpdate(ValueAnimator animation) {
        mValueAvatar = (Float) animation.getAnimatedValue("rotation");
      }
    });
    mAniAvatar.setDuration(ROTATE_TIME);
    mAniAvatar.setInterpolator(new LinearInterpolator());
    mAniAvatar.setRepeatCount(ROTATE_COUNT);
  }

}
